//
//  QJMineCollectVC.m
//  QuanJieShopProject
//
//  Created by 刘中义 on 2018/2/24.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "QJMineCollectVC.h"
#import "QJPreviewOrderCell.h"
#import "QJShopDetailVC.h"
#import "UIScrollView+EmptyDataSet.h"
@interface QJMineCollectVC () <UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetDelegate,DZNEmptyDataSetSource>
@property (nonatomic, strong) UITableView *collectTableView;
@property (nonatomic, strong) NSMutableArray *dataSourceArray;

@end

@implementation QJMineCollectVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的收藏";
    [self setupCollectUIAction];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavAlpha:YES];
    //控件不被导航栏遮挡
    self.navigationController.navigationBar.translucent = NO;
    //数据请求
    [self dataRequestAction];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self setNavAlpha:NO];
    self.navigationController.navigationBar.translucent = YES;
}
#pragma mark -- 界面布局
- (void)setupCollectUIAction{
    
    CGFloat top = IOS_VERSION < 11.0 ? Height_NavBar : 0;
    self.collectTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, top, UISCREEN_WIDTH, UISCREEN_HEIGHT - top) style:UITableViewStylePlain];
    [self.view addSubview:self.collectTableView];
    self.collectTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.collectTableView.delegate = self;
    self.collectTableView.dataSource = self;
    self.collectTableView.emptyDataSetDelegate = self;
    self.collectTableView.emptyDataSetSource = self;
    
    self.extendedLayoutIncludesOpaqueBars = YES;
    if (IOS_VERSION < 11.0) {
        //解决表无故偏移
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}
#pragma mark -- 数据请求
- (void)dataRequestAction{
    
    // "    http://www.quanjieshop.com/index.php/api/AppUser/goods_collect
    [WWNetRequest fetchCollectListWithParameters:nil success:^(id response) {
        if (response) {
            NSLog(@"收藏列表response = %@", response);
            NSArray *arr = response[@"data"][@"result"];
            NSMutableArray *array = [NSMutableArray array];
            [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                MineCollectModel *model = [[MineCollectModel alloc]initWithDictionary:obj error:nil];
                [array addObject:model];
            }];
            self.dataSourceArray = array;
            [self.collectTableView reloadData];
        }
    } failure:^(NSDictionary *error) {
        
    }];
}
#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"cellID";
    QJPreviewOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"QJPreviewOrderCell" owner:self options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    MineCollectModel *model = self.dataSourceArray[indexPath.row];
    [cell cellUpdateWithMineCollectModel:model];
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    QJShopDetailVC *VC = [QJShopDetailVC new];
    MineCollectModel *model = self.dataSourceArray[indexPath.row];
    VC.goodsID = model.goodsid;
    KPushViewControllerAction(VC);
}
#pragma mark -- DZNEmptyDataSetSource
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return [UIImage imageNamed:@"noDataImage"];
}
#pragma mark -- DZNEmptyDataSetDelegate
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
    return YES;
}
#pragma mark -- 懒加载
- (NSMutableArray *)dataSourceArray{
    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray array];
    }
    return _dataSourceArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
